package com.jinxinol.basic.system.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysOrgan;
import com.jinxinol.basic.system.domain.SysPosition;
import com.jinxinol.basic.system.service.ISysOrganService;
import com.jinxinol.basic.system.service.ISysPositionService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author Chenbing
 * @email wwwchenbing@gmail.com
 * @description 部门及职务管理Action
 */
public class OrgPosAction extends BaseAction {
	private static final long serialVersionUID = -8055720037067108199L;
	private static final Log logger = ContextHelper.getLogger(OrgPosAction.class);
	
	private SysOrgan org = new SysOrgan();
	private SysPosition pos = new SysPosition();
	
	private List<SysOrgan> orgList = new ArrayList<SysOrgan>();
	private List<SysPosition> posList = new ArrayList<SysPosition>();
	
	@Resource 
	ISysOrganService organService;
	@Resource
	ISysPositionService posService;
	
	public OrgPosAction(){}
	
	/**
	 * 加载部门信息数据
	 */
	public void loadOrg(){
		try {
			orgList = organService.find(org);
			
			jsonObject.put(RESULT_CODE, SUCCESS);
			jsonObject.put("list", orgList);
		} catch (Exception e) {
			logger.error("加载部门信息失败！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		
		this.pushJsonResult();
	}
	
	/**
	 * 创建组织部门
	 * @return
	 */
	public String insertOrg() throws Exception{
		try {
			org.setO_level(org.getF_level() + 1);
			org.setO_code("ODC" + System.currentTimeMillis());
			organService.insert(org);
			
			return "insertOrg_success";
		} catch (Exception e) {
			logger.error("创建组织部门失败！", e);
			throw e;
		}
	}
	
	/**
	 * 更新组织部门
	 * @return
	 */
	public String updateOrg() throws Exception{
		try {
			organService.update(org);		
			
			return "updateOrg_success";
		} catch (Exception e) {
			logger.error("更新组织部门失败！", e);
			throw e;
		}
	}
	
	/**
	 * 加载部门职务信息
	 * @return
	 * @throws Exception
	 */
	public String loadPosition()throws Exception{
		try {
			if(pos.getOrg_id() != null){
				posList = posService.find(pos);
				org = organService.findById(pos.getOrg_id());
			}

			return "loadPosition_success";
		} catch (Exception e) {
			logger.error("更新组织部门失败！", e);
			throw e;
		}
	}
	
	/**
	 * 以JSON格式返回职务结果集
	 */
	public void loadPositionJSON(){
		try {
			if(pos.getOrg_id() != null){
				posList = posService.find(pos);
				pos.setIs_active("1"); //查询"启用"状态下的职务
				org = organService.findById(pos.getOrg_id());
			}
			
			jsonObject.put(RESULT_CODE, SUCCESS);
			jsonObject.put("list", posList);
		} catch (Exception e) {
			logger.error("更新职务状态失败！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		
		this.pushJsonResult();
	}
	
	/**
	 * 创建职务
	 * @return
	 * @throws Exception
	 */
	public String insertPosition() throws Exception{
		try {
			pos.setIs_active("1");	//创建时默认为1生效
			pos.setCreated(new Date());
			pos.setCreatedby(sysUser.getId());
			
			posService.insert(pos);
			
			return "insertPosition_success";
		} catch (Exception e) {
			logger.error("创建职务失败！", e);
			throw e;
		}
	}
	
	/**
	 * 更新职务
	 */
	public String updatePosition() throws Exception{
		try {
			pos.setUpdated(new Date());
			pos.setUpdatedby(sysUser.getId());
			posService.update(pos);
			
			return "updatePosition_success";
		} catch (Exception e) {
			logger.error("更新职务失败！", e);
			throw e;
		}
	}
	
	/**
	 * 更新职务状态
	 */
	public void updatePositionState(){
		try {
			//设置失效时间
			if(pos.getIs_active().equals("2")){
				pos.setDis_date(new Date());
			}else{
				pos.setDis_date(null);
			}
			
			posService.update(pos);
			
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("更新职务状态失败！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		
		this.pushJsonResult();
	}
	
	public void deletePosition(){
		try {
			//检查是否可以删除该职务

			posService.delete(pos);
			
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("更新职务状态失败！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		
		this.pushJsonResult();
	}
	
	/**
	 * 检查组织部门名称唯一合法性
	 */
	public void checkOrg(){
		try {
			String valid = "true";
			String checkType = (String)request.getParameter("checkType");
			
			if(checkType != null && checkType.equals("insert")){
				//新增时对名称进行唯一性验证
				List<SysOrgan> ol = organService.find(org);
				valid = ol.size()==0 ? "true" : "false";
			}else if(checkType != null && checkType.equals("update")){
				//更新时对名称进行唯一性验证
				Integer orgId = org.getId();
				org.setId(null);
				List<SysOrgan> ol = organService.find(org);
				
				for(SysOrgan so : ol){
					if(!so.getId().equals(orgId)){
						valid = "false";
						break;
					}
				}
			}
			
			jsonObject.put("valid", valid);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("检查组织名称合法性出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		this.pushJsonResult();
	}
	
	/**
	 * 检查职务名称唯一合法性
	 */
	public void checkPos(){
		try {
			String valid = "true";
			String checkType = (String)request.getParameter("checkType");
			
			if(checkType != null && checkType.equals("insert")){
				//新增时对名称进行唯一性验证
				List<SysPosition> pl = posService.find(pos);
				valid = pl.size()==0 ? "true" : "false";
			}else if(checkType != null && checkType.equals("update")){
				//更新时对名称进行唯一性验证
				Integer posId = pos.getPosition_id();
				pos.setPosition_id(null);
				List<SysPosition> pl = posService.find(pos);
				
				for(SysPosition sp : pl){
					if(!sp.getPosition_id().equals(posId)){
						valid = "false";
						break;
					}
				}
			}
			
			jsonObject.put("valid", valid);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("检查职务名称合法性出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		this.pushJsonResult();
	}

	public SysOrgan getOrg() {
		return org;
	}

	public void setOrg(SysOrgan org) {
		this.org = org;
	}

	public List<SysOrgan> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<SysOrgan> orgList) {
		this.orgList = orgList;
	}

	public SysPosition getPos() {
		return pos;
	}

	public void setPos(SysPosition pos) {
		this.pos = pos;
	}

	public List<SysPosition> getPosList() {
		return posList;
	}

	public void setPosList(List<SysPosition> posList) {
		this.posList = posList;
	}
}
